﻿<# SILA : Shibboleth IdP Loader For ADFS

 SILA is a script that I wanted to be simple for an easy adaptation.
 
 Installation : 
 The script should be copy on the ADFS server
 A web folder should be added to the ADFS Server
 the temp folder must be set with read right for everyone

 Lesson learned :
 IdP should have a unique SAML signing key
 Use absolute path, and change to to script folder when running in batch.

 (c) CNRS - 2012
 licence : CeCiLL B
 
# jmt-01oct2012 : create
# jmt-08nov2012-0.1 : add comment for release... It is still a working job
# jmt-10/11/12-0.2 : check validUntil value
# jmt-15/11/12-0.03 : check metdata signature
# jmt-08aug13-0.4 : reset acl for each file on the web server
# jmt-08aug13-0.5 : add copy of metadatafile to the server of the farm
# jmt-08aug13-0.6 : copy RHD file to servers with a backup
# jmt-23oct13-0.7 : split the initial script to have all configuration in the original file.
#                   SILA.ps1 = configuration file and entry point
#                   SILAmain.ps1 = main program
#                   SILAhelper.ps1 = helper function
#>
function discoJsonFromFile() {

    $file = "D:\Work\Projects\CORE\Scripts\sila\temp\aai-idp.unibe.ch.xml"
    $discoJsonFile = "D:\Work\Projects\CORE\Scripts\sila\temp\aai-idp.unibe.ch.json"
    
    $xmldata = new-object Xml.XmlDocument
    $xmldata.PreserveWhitespace = $true
    $xmldata.Load($file)

    $entity = $xmldata.EntityDescriptor

    discoJson -entity $entity
}

function discoJson {
    param (
        $entity,
        $idpCount
    )

    if ($idpCount -gt 0 ) {
        "," |  out-file -encoding:UTF8 $discoJsonFile -append
    }

    # displayNames is mandatoy so at least the value wil be the entityID 
    "{" |  out-file -encoding:UTF8 $discoJsonFile -append
    '"entityID": "'+ $entity.EntityID + '",' |  out-file -encoding:UTF8 $discoJsonFile -append

    '"DisplayNames": [' |  out-file -encoding:UTF8 $discoJsonFile -append

    if ( $entity.IDPSSODescriptor.Extensions.UIInfo.DisplayName -ne $null ) {
        doValueLang $entity.IDPSSODescriptor.Extensions.UIInfo.DisplayName    
    } else {
        if ($entity.Organization.OrganizationDisplayName -ne $null) {
            doValueLang $entity.Organization.OrganizationDisplayName    
        } else {
            if ($entity.Organization.OrganizationName -ne $null) {
                doValueLang $entity.Organization.OrganizationName
            } else {
                '{ "value": "' + $entity.EnityID + '",' |  out-file -encoding:UTF8 $discoJsonFile -append
                '"lang": "en" }' |  out-file -encoding:UTF8 $discoJsonFile -append

            }
        }
    }
    "]" |  out-file -encoding:UTF8 $discoJsonFile -append

    if ( $entity.IDPSSODescriptor.Extensions.UIInfo.Description -ne $null ) {
        "," |  out-file -encoding:UTF8 $discoJsonFile -append
        '"Descriptions": [' |  out-file -encoding:UTF8 $discoJsonFile -append
        doValueLang $entity.IDPSSODescriptor.Extensions.UIInfo.Description    
        "]" |  out-file -encoding:UTF8 $discoJsonFile -append
    }
    
    if ( $entity.IDPSSODescriptor.Extensions.UIInfo.InformationURL -ne $null ) {
        "," |  out-file -encoding:UTF8 $discoJsonFile -append
        '"InformationURLs": [' |  out-file -encoding:UTF8 $discoJsonFile -append
        doValueLang $entity.IDPSSODescriptor.Extensions.UIInfo.InformationURL    
        "]" |  out-file -encoding:UTF8 $discoJsonFile -append
    }

    if ( $entity.IDPSSODescriptor.Extensions.UIInfo.Logo -ne $null ) {
        "," |  out-file -encoding:UTF8 $discoJsonFile -append
        '"Logos": [' |  out-file -encoding:UTF8 $discoJsonFile -append
        doLogo $entity.IDPSSODescriptor.Extensions.UIInfo.Logo    
        "]" |  out-file -encoding:UTF8 $discoJsonFile -append
    }

    "}" |  out-file -encoding:UTF8 $discoJsonFile -append
     
}

function doValueLang {
<#
    process value lang data
#>
    param (
        $items
    )

    $i = 0
    foreach ($item in $items ){
        $val = $item.InnerText.Trim()
        $val = $val.Replace("`n","\\n")
        $val = $val.Replace("`r","\\r")
        $val = $val.Replace("`t","\\t")

        $i ++
        "{" |  out-file -encoding:UTF8 $discoJsonFile -append
        '"value": "' + $val + '",' |  out-file -encoding:UTF8 $discoJsonFile -append
        '"lang": "' + $item.lang +'"' |  out-file -encoding:UTF8 $discoJsonFile -append
        if ($i -lt $items.Count ) {
            "}," |  out-file -encoding:UTF8 $discoJsonFile -append
        } else {
            "}" |  out-file -encoding:UTF8 $discoJsonFile -append
        }
    }
}

function doLogo {
<#
    process value lang data
#>
    param (
        $items
    )

    $i = 0
    foreach ($item in $items ){
        $i ++
        "{" |  out-file -encoding:UTF8 $discoJsonFile -append
        '"value": "' + $item.InnerText.trim() + '",' |  out-file -encoding:UTF8 $discoJsonFile -append
        '"height": "' + $item.height +'",' |  out-file -encoding:UTF8 $discoJsonFile -append
        '"width": "' + $item.width +'"' |  out-file -encoding:UTF8 $discoJsonFile -append
        if ($i -lt $items.Count ) {
            "}," |  out-file -encoding:UTF8 $discoJsonFile -append
        } else {
            "}" |  out-file -encoding:UTF8 $discoJsonFile -append
        }
    }
}

function openDiscoJson {
    "["  |  out-file -encoding:UTF8 $discoJsonFile
}

function closeDiscoJson {
    "]"  |  out-file -encoding:UTF8 $discoJsonFile -Append
}
